package org.kjtc.init;

import org.kjtc.service.WxMpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 项目启动执行
 * Created by xiaojin on 2019/1/5/005.
 */
@Component //被spring容器管理
@Order(1)
public class InitProjectredis implements ApplicationRunner {

    private static Logger log = LoggerFactory.getLogger(InitProjectredis.class);
    @Autowired
    private WxMpService wxMpService;
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("-------------->" + "执行缓存每小时执行一次的任务redis，now=" + new Date());
        myTimer();
    }

    public   void myTimer(){
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                log.info("开始token");
                wxMpService.creatAccrssToken();
                log.info("结束token");
            }
        };

        //设置执行时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
        //定制每天的21:09:00执行，
        calendar.set(year, month, day, 00, 00, 00);
        Date date = calendar.getTime();
        Timer timer = new Timer();
        System.out.println(date);

        int period = 60 * 60 * 1000;
        //每天的date时刻执行task，每隔1小时重复执行
        timer.schedule(task, date, period);
    }
}
